package com.ccyl.servlet;

import com.ccyl.entity.Resource;
import com.ccyl.repository.ResourceRepository;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getResource")
public class ResourceServlet extends HttpServlet {
    private ResourceRepository resourceRepository=new ResourceRepository();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method=req.getParameter("method");
        if (method == null) {
            method="findAll";
        }
        switch (method) {
            case "findAll":
                //在客户端返回视图+数据
                //super.doGet(req, resp);
                List<Resource> list=resourceRepository.findAll();
                req.setAttribute("list",list);
                req.getRequestDispatcher("getResource.jsp").forward(req,resp);
                break;
            case "delete":
                //在这里找到元素，随后进入Repository方法中将其删除。
                String seasonStr=req.getParameter("season");
                String episodeStr=req.getParameter("episode");
                //Repository类中的删除方法将其删除。方法体在下面。
                resourceRepository.deleteBySeasonAndEpisode(seasonStr,episodeStr);
                //删除完成后继续进行重定向
                resp.sendRedirect("getResource");
                break;
            case "findItem":
                seasonStr=req.getParameter("season");
                episodeStr=req.getParameter("episode");
                //Repository类中的查找
                req.setAttribute("resource",resourceRepository.findItem(seasonStr,episodeStr));
                //执行修改操作之后，进入update页面进行修改操作。
                req.getRequestDispatcher("update.jsp").forward(req,resp);
                break;
            case "searchItem":
                seasonStr=req.getParameter("season");
                episodeStr=req.getParameter("episode");
                //Repository类中的查找
                //判断提供参数是否为空
                if (resourceRepository.findItem(seasonStr,episodeStr)==null) {
                    resp.sendRedirect("errorfound.jsp");
                }
                else {
                    req.setAttribute("resource",resourceRepository.findItem(seasonStr,episodeStr));
                    //执行修改操作之后，test1页面进入。
                    // System.out.println(resourceRepository.findItem(seasonStr,episodeStr).toString());
                    req.getRequestDispatcher("information.jsp").forward(req,resp);
                }

                //req.setAttribute("resource",resourceRepository.findItem(seasonStr,episodeStr));
                //执行修改操作之后，test1页面进入。
               // System.out.println(resourceRepository.findItem(seasonStr,episodeStr).toString());
                //req.getRequestDispatcher("information.jsp").forward(req,resp);
                break;
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
        req.setCharacterEncoding("utf-8");
        String method=req.getParameter("method");
        switch (method) {
            case "add":
                String seasonStr = req.getParameter("season");
                String episodeStr = req.getParameter("episode");
                String endImageURL=req.getParameter("endImageURL");
                String videoURL=req.getParameter("videoURL");
                //Add item to database.
                resourceRepository.add(seasonStr,episodeStr,endImageURL,videoURL);
                break;
            case "update":
                seasonStr = req.getParameter("season");
                episodeStr = req.getParameter("episode");
                endImageURL=req.getParameter("endImageURL");
                videoURL=req.getParameter("videoURL");
                //Update func.
                resourceRepository.updateResource(seasonStr,episodeStr,endImageURL,videoURL);
                break;
        }
        resp.sendRedirect("getResource");
    }
}
